package com.onlyphoto.onlymall.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.onlyphoto.onlymall.R;


/**
 * Created by gaochujia on 2019/7/24.
 * 常规显示列表条目
 */

public class NormalItemView extends LinearLayout {

    /**
     * item icon
     */
    private ImageView mIcon, mRightArrow;
    /**
     * item title
     */
    private TextView mTitle, mSubTitle;

    private LinearLayout ll_sub;

    private View mLine;

    private int titleColor = ContextCompat.getColor(getContext(), R.color.text_color);
    private int subTitleColor = ContextCompat.getColor(getContext(), R.color.title_color);

    private String title, subTitle;
    private Bitmap icon;

    private boolean isShow = true;

    private boolean isArrowGone = false;

    public NormalItemView(Context context) {
        this(context, null);
    }

    public NormalItemView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NormalItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initTypeValue(context, attrs, defStyleAttr);
        initView();
    }

    /**
     * 初始化对应属性
     *
     * @param context      桑下文
     * @param attrs        AttributeSet
     * @param defStyleAttr int
     */
    private void initTypeValue(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.NormalItemViewStyle, defStyleAttr, 0);
        int indexCount = array.getIndexCount();
        for (int i = 0; i < indexCount; i++) {
            int attr = array.getIndex(i);
            switch (attr) {
                case R.styleable.NormalItemViewStyle_itemIcon:
                    icon = BitmapFactory.decodeResource(getResources(), array.getResourceId(attr, 0));
                    break;
                case R.styleable.NormalItemViewStyle_itemTitle:
                    title = array.getString(attr);
                    break;
                case R.styleable.NormalItemViewStyle_itemSubTitle:
                    subTitle = array.getString(attr);
                    break;
                case R.styleable.NormalItemViewStyle_itemDivideLine:
                    isShow = array.getBoolean(attr, true);
                    break;
                case R.styleable.NormalItemViewStyle_itemTitleColor:
                    titleColor = array.getColor(attr, ContextCompat.getColor(getContext(), R.color.text_color));
                    break;
                case R.styleable.NormalItemViewStyle_subItemTitleColor:
                    subTitleColor = array.getColor(attr, ContextCompat.getColor(getContext(), R.color.title_color));
                    break;
                case R.styleable.NormalItemViewStyle_itemArrowGone:
                    isArrowGone = array.getBoolean(attr, false);
                default:
                    break;
            }
        }
    }


    /**
     * 初始化对应控件
     */
    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_normal_item, this);
        mIcon = findViewById(R.id.item_icon);
        mTitle = findViewById(R.id.item_title);
        mSubTitle = findViewById(R.id.item_subtitle);
        mRightArrow = findViewById(R.id.img_arrow_right);
        ll_sub = findViewById(R.id.ll_sub);
        mLine = findViewById(R.id.view_line);

        if (icon != null) {
            mIcon.setVisibility(View.GONE);
            mIcon.setImageBitmap(icon);
        } else {
            mIcon.setVisibility(View.GONE);
        }

        mTitle.setTextColor(titleColor);
        mSubTitle.setTextColor(subTitleColor);
        if (!TextUtils.isEmpty(title)) {
            mTitle.setText(title);
        }

        if (!TextUtils.isEmpty(subTitle)) {
            mSubTitle.setText(subTitle);
        }
        mRightArrow.setVisibility(isArrowGone ? View.GONE : View.VISIBLE);
        mLine.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    /**
     * 获取item icon
     *
     * @return ImageView
     */
    public ImageView getIconView() {
        if (View.VISIBLE != mIcon.getVisibility()) {
            mIcon.setVisibility(View.VISIBLE);
        }
        return mIcon;
    }

    /**
     * get item subTitle view
     *
     * @return String
     */
    public String getSubTitle() {
        return mSubTitle.getText().toString();
    }

    /**
     * get item title view
     *
     * @return TextView
     */
    public TextView getTitleView() {
        return mTitle;
    }

    /**
     * item 设置标签
     *
     * @param title
     */
    public void setTitle(String title) {
        mTitle.setText(title);
    }

    /**
     * 设置右边TextView显示
     *
     * @param subTitle String
     */
    public void setSubTitle(String subTitle) {
        mSubTitle.setText(subTitle);
    }

    /**
     * 获取副标题 控件
     *
     * @return TextView
     */
    public TextView getSubTitleView() {
        return mSubTitle;
    }

    /**
     * 去掉右边的尖头
     */
    public void setRightArrowGone() {
        mRightArrow.setVisibility(View.GONE);
    }

    /**
     * 设置点击事件
     */

    public void click(OnClickListener listener) {
        ll_sub.setOnClickListener(listener);
    }

}
